package com.ruoyi.result.home.service.impl;


import com.ruoyi.result.awardPersonal.mapper.ResultPersonalAwardMapper;
import com.ruoyi.result.awardStudent.mapper.ResultStudentAwardMapper;
import com.ruoyi.result.certificateinfo.mapper.ResultCertificateinfoMapper;
import com.ruoyi.result.copyright.mapper.ResultCopyrightMapper;
import com.ruoyi.result.home.domain.dto.AchievementTrendDTO;
import com.ruoyi.result.home.service.AchievementTrendService;
import com.ruoyi.result.horizontalProjects.mapper.ResultHorizontalProjectsMapper;
import com.ruoyi.result.longitudinalProjects.mapper.ResultLongitudinalProjectsMapper;
import com.ruoyi.result.material.mapper.ResultContinuingEducationMaterialMapper;
import com.ruoyi.result.other.mapper.ResultOtherMapper;
import com.ruoyi.result.papers.mapper.ResultPapersMapper;
import com.ruoyi.result.patents.mapper.ResultPatentsMapper;
import com.ruoyi.result.publication.mapper.ResultPublicationMapper;
import com.ruoyi.result.report.mapper.ResultReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class AchievementTrendServiceImpl implements AchievementTrendService {

    @Autowired
    private ResultPapersMapper papersMapper;

    @Autowired
    private ResultPatentsMapper patentsMapper;

    @Autowired
    private ResultCopyrightMapper copyrightMapper;

    @Autowired
    private ResultPublicationMapper publicationMapper;

    @Autowired
    private ResultPersonalAwardMapper personalAwardMapper;

    @Autowired
    private ResultStudentAwardMapper studentAwardMapper;

    @Autowired
    private ResultReportMapper reportMapper;

    @Autowired
    private ResultCertificateinfoMapper certificateinfoMapper;

    @Autowired
    private ResultContinuingEducationMaterialMapper educationMaterialMapper;

    @Autowired
    private ResultHorizontalProjectsMapper horizontalProjectsMapper;

    @Autowired
    private ResultLongitudinalProjectsMapper longitudinalProjectsMapper;

    @Autowired
    private ResultOtherMapper otherMapper;

    @Override
    public AchievementTrendDTO getAllAchievementTrend() {
        // 获取各类成果的年度统计数据
        List<Map<String, Object>> paperCounts = papersMapper.countByYear();
        List<Map<String, Object>> patentCounts = patentsMapper.countByYear();
        List<Map<String, Object>> copyrightCounts = copyrightMapper.countByYear();
        List<Map<String, Object>> publicationCounts = publicationMapper.countByYear();
        List<Map<String, Object>> personalAwardCounts = personalAwardMapper.countByYear();
        List<Map<String, Object>> studentAwardCounts = studentAwardMapper.countByYear();
        List<Map<String, Object>> reportCounts = reportMapper.countByYear();
        List<Map<String, Object>> certificateCounts = certificateinfoMapper.countByYear();
        List<Map<String, Object>> educationMaterialCounts = educationMaterialMapper.countByYear();
        List<Map<String, Object>> horizontalProjectCounts = horizontalProjectsMapper.countByYear();
        List<Map<String, Object>> longitudinalProjectCounts = longitudinalProjectsMapper.countByYear();
        List<Map<String, Object>> otherCounts = otherMapper.countByYear();

        // 整合所有年份
        Set<Integer> yearSet = new HashSet<>();
        addYearsToSet(yearSet, paperCounts);
        addYearsToSet(yearSet, patentCounts);
        addYearsToSet(yearSet, copyrightCounts);
        addYearsToSet(yearSet, publicationCounts);
        addYearsToSet(yearSet, personalAwardCounts);
        addYearsToSet(yearSet, studentAwardCounts);
        addYearsToSet(yearSet, reportCounts);
        addYearsToSet(yearSet, certificateCounts);
        addYearsToSet(yearSet, educationMaterialCounts);
        addYearsToSet(yearSet, horizontalProjectCounts);
        addYearsToSet(yearSet, longitudinalProjectCounts);
        addYearsToSet(yearSet, otherCounts);

        // 排序年份
        List<Integer> years = new ArrayList<>(yearSet);
        Collections.sort(years);

        // 构建返回数据
        Map<String, List<Integer>> data = new HashMap<>();
        data.put("论文", convertToYearlyData(years, paperCounts));
        data.put("专利", convertToYearlyData(years, patentCounts));
        data.put("软件著作权", convertToYearlyData(years, copyrightCounts));
        data.put("著作", convertToYearlyData(years, publicationCounts));
        data.put("个人获奖", convertToYearlyData(years, personalAwardCounts));
        data.put("学生获奖", convertToYearlyData(years, studentAwardCounts));
        data.put("新闻报道", convertToYearlyData(years, reportCounts));
        data.put("档案资料", convertToYearlyData(years, certificateCounts));
        data.put("继续教育材料", convertToYearlyData(years, educationMaterialCounts));
        data.put("横向课题", convertToYearlyData(years, horizontalProjectCounts));
        data.put("纵向课题", convertToYearlyData(years, longitudinalProjectCounts));
        data.put("其他", convertToYearlyData(years, otherCounts));

        AchievementTrendDTO result = new AchievementTrendDTO();
        result.setYears(years);
        result.setData(data);

        return result;
    }

    @Override
    public AchievementTrendDTO getUserAchievementTrend(Long userId) {
        // 获取指定用户各类成果的年度统计数据
        List<Map<String, Object>> paperCounts = papersMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> patentCounts = patentsMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> copyrightCounts = copyrightMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> publicationCounts = publicationMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> personalAwardCounts = personalAwardMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> studentAwardCounts = studentAwardMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> reportCounts = reportMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> certificateCounts = certificateinfoMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> educationMaterialCounts = educationMaterialMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> horizontalProjectCounts = horizontalProjectsMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> longitudinalProjectCounts = longitudinalProjectsMapper.countByYearAndUserId(userId);
        List<Map<String, Object>> otherCounts = otherMapper.countByYearAndUserId(userId);

        // 整合所有年份
        Set<Integer> yearSet = new HashSet<>();
        addYearsToSet(yearSet, paperCounts);
        addYearsToSet(yearSet, patentCounts);
        addYearsToSet(yearSet, copyrightCounts);
        addYearsToSet(yearSet, publicationCounts);
        addYearsToSet(yearSet, personalAwardCounts);
        addYearsToSet(yearSet, studentAwardCounts);
        addYearsToSet(yearSet, reportCounts);
        addYearsToSet(yearSet, certificateCounts);
        addYearsToSet(yearSet, educationMaterialCounts);
        addYearsToSet(yearSet, horizontalProjectCounts);
        addYearsToSet(yearSet, longitudinalProjectCounts);
        addYearsToSet(yearSet, otherCounts);

        // 排序年份
        List<Integer> years = new ArrayList<>(yearSet);
        Collections.sort(years);

        // 构建返回数据
        Map<String, List<Integer>> data = new HashMap<>();
        data.put("论文", convertToYearlyData(years, paperCounts));
        data.put("专利", convertToYearlyData(years, patentCounts));
        data.put("软件著作权", convertToYearlyData(years, copyrightCounts));
        data.put("著作", convertToYearlyData(years, publicationCounts));
        data.put("个人获奖", convertToYearlyData(years, personalAwardCounts));
        data.put("学生获奖", convertToYearlyData(years, studentAwardCounts));
        data.put("新闻报道", convertToYearlyData(years, reportCounts));
        data.put("档案资料", convertToYearlyData(years, certificateCounts));
        data.put("继续教育材料", convertToYearlyData(years, educationMaterialCounts));
        data.put("横向课题", convertToYearlyData(years, horizontalProjectCounts));
        data.put("纵向课题", convertToYearlyData(years, longitudinalProjectCounts));
        data.put("其他", convertToYearlyData(years, otherCounts));

        AchievementTrendDTO result = new AchievementTrendDTO();
        result.setYears(years);
        result.setData(data);

        return result;
    }

    /**
     * 将年份添加到集合中
     */
    private void addYearsToSet(Set<Integer> yearSet, List<Map<String, Object>> counts) {
        for (Map<String, Object> count : counts) {
            yearSet.add(((Number) count.get("year")).intValue());
        }
    }

    /**
     * 将数据库查询结果转换为按年份排列的数据列表
     */
    private List<Integer> convertToYearlyData(List<Integer> years, List<Map<String, Object>> counts) {
        Map<Integer, Integer> yearCountMap = counts.stream()
                .collect(Collectors.toMap(
                        count -> ((Number) count.get("year")).intValue(),
                        count -> ((Number) count.get("count")).intValue()
                ));

        return years.stream()
                .map(year -> yearCountMap.getOrDefault(year, 0))
                .collect(Collectors.toList());
    }
}

